<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use fast\QRcode;
use ZipArchive;
use DB;

/**
 * 
 *
 * @icon fa fa-users
 */
class Users extends Backend {

    /**
     * Users模型对象
     * @var \app\admin\model\Users
     */
    protected $model = null;

    public function _initialize() {
        parent::_initialize();
        $this->model = model('Users');
        $this->view->assign("sexList", $this->model->getSexList());
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    public function detail($ids) {
        $row = $this->model->get(['user_id' => $ids]);
        if (!$row) {
            $this->error(__('No Results were found'));
        }

        if (empty($row['brithday'])) {
            $row['age'] = '';
        } else {
            $row['age'] = $this->birthday($row['brithday']);
        }

        $carmodel = model('app\admin\model\Car');
//        echo $row['user_id'];die;
        $car_info = $carmodel->where('user_id', $row['user_id'])->select();
//        echo $carmodel->getLastSql();
        if ($car_info) {
            $row['car_info'] = $car_info;
        } else {
            $row['car_info'] = [];
        }
        $this->view->assign("info", $row->toArray());
        $this->view->assign("sexList", $this->model->getSexList());
        return $this->view->fetch();
    }

    public function birthday($birthday) {
        $age = strtotime($birthday);
        if ($age === false) {
            return false;
        }
        list($y1, $m1, $d1) = explode("-", date("Y-m-d", $age));
        $now = strtotime("now");
        list($y2, $m2, $d2) = explode("-", date("Y-m-d", $now));
        $age = $y2 - $y1;
        if ((int) ($m2 . $d2) < (int) ($m1 . $d1))
            $age -= 1;
        return $age;
    }

    public function picture() {
        $start = $this->request->param('start', 0);
        $end = $this->request->param('end', 0);

        if ($start == 0) {
            $this->error('错误的开始参数');
        }

        if ($end == 0) {
            $this->error('错误的结束参数');
        }

        $i = 0;
        $name = '';
        $img = [];
        $fileList = [];
        for ($k = $start; $k <= $end; $k++) {


            $img[] = $this->qrcode($k);
            $i++;
            $name .= $k . '-';
            $number[] = str_pad($k, 12, 0, STR_PAD_LEFT);
            //三张二维码生成一张图
            if ($i == 3) {
                watermarkLocal($img, $name, $number);
                $fileList[] = './uploads/car/' . $name . '.jpg';

                //删除二维码图
                foreach ($img as $val) {
                    unlink($val);
                }
                $i = 0;
                $name = '';
                $img = [];
                $number = [];
            }
        }


        //打包
        $filename = "./uploads/zip/" . $start . 'to' . $end . ".zip";
        $zip = new ZipArchive();
        $zip->open($filename, ZipArchive::CREATE);   //打开压缩包
        foreach ($fileList as $file) {
            $zip->addFile($file, basename($file));   //向压缩包中添加文件
        }


        $zip->close();  //关闭压缩包
        //删除原图
        foreach ($fileList as $file) {
            unlink($file);
        }
        $this->redirect($filename, 200);
    }

    /**
     * host 生成链接地址
     * level 容错级别 
     * size 图片大小
     */
    public function qrcode($card_number = '', $level = 3, $size = 10) {

        $errorCorrectionLevel = intval($level); //容错级别 
        $matrixPointSize = intval($size); //生成图片大小 
        $url = 'https://lc.feiyouce.com/index.php?s=/Qcode/index/id/' . $card_number;

        //生成二维码图片 
        $object = new QRcode();
        $fileName = './uploads/car/' . $card_number . '.png';

        $res = $object->png($url, $fileName, $errorCorrectionLevel, $matrixPointSize, 2, false);
//        $QR = imagecreatefromstring(file_get_contents($fileName));
//
//        //输出图片    
//        imagepng($QR, $card_number . '.png');
//        imagedestroy($QR);
        return $fileName;
    }

}
